namespace LinqRepositorySample.Domain
{
    public class OrderDetail
    {
        #region fields
        private int _orderDetailId;
        private Order _order;
        private Product _product;
        private decimal _price;
        private int _qty;
        #endregion

        #region properties
        public virtual int OrderDetailId
        {
            get { return _orderDetailId; }
        }

        public virtual Order Order
        {
            get { return _order; }
            internal set
            {
                OrderId = 0;
                _order = value;
                if (_order != null)
                    OrderId = _order.OrderId;
                
            }
        }

        public virtual int OrderId
        {
            get;
            set;
        }

        public virtual Product Product
        {
            get { return _product; }
            set
            {
                _product = value;
                if (_product == null)
                {
                    ProductId = 0;
                    Qty = 0;
                    Price = 0;
                } else
                {
                    ProductId = _product.ProductId;
                    Qty = 1;
                    Price = _product.StandardPrice;
                }
            }
        }

        public virtual int ProductId { get; set; }

        public virtual int Qty
        {
            get { return _qty; }
            set
            {
                _qty = value;
                Total = Qty * Price;
            }
        }

        public virtual decimal Price {
            get { return _price; }
            set
            {
                _price = value;
                Total = Qty*_price;
            }
        }

        public virtual decimal Total
        {
            get; set;
        }
        #endregion

    }
}